<?php
class ExpandAction extends CommonAction {
//构造函数  5.3  封疆
	public $user;
	public $role_power;
	public $role;
	public $power;
	public $topics;
	public $safe_question;
	public function __construct() 
	{
		parent::__construct();
//		$uid = $this->checkLoginStatus();
//		if($uid!=1)
//		{
//			$this->Successs('无权限操作！','',0);
//		}
		$this->user = M('web_user');
		$this->role_power = M('web_role_power');
		$this->role = M('web_role');
		$this->power = M('web_power');
		$this->configs = M('web_config');
		$this->topics = M('web_hot_topic');
		$this->integral_setting = M('web_integral_setting');
		$this->safe_question = M('web_safe_question');
		$this->about_us = M('web_about_us');
	}
        // 首页
        
	public function kzsm()
	{
		if($id = $this->_get('id')){
			$power = $this->power->where('id='.$id)->field('memo')->find();
			//print_r($power);
			$this->assign('power',$power['memo']);	
		}
		$this->display();
	}
	public function Index() 
	{
		$this->display();
	}
	
	public function Authoritydel()
	{
		if($id = $this->_get('id')){
			$power = $this->power->where('id='.$id)->find();
			$powers = $this->power->where('parent='.$id)->find();
			if($powers['id'])
			{
					$this->admin_log('del','power','无','0');//日志文件
					//$this->Successs('请删除子分类','/Admin/index.php/Expand/Authority',0);
					$this->error ('请删除子分类！');
			}
			if($power['id']){
				$powers = $this->power->where('id='.$power['id'])->delete();
				if($powers){
					$role_power = $this->role_power->where('power_id='.$id)->delete();
				}
				if(false !== $powers AND false !== $role_power)
				{
					$this->admin_log('del','power,role_power','无','1');//日志文件
					$this->success ('数据写入成功！');
				}
				else
				{
					$this->admin_log('del','power','无','0');//日志文件
					$this->error('数据写入错误！');
				}
			}else{
				$this->error('无效提交！');
			}
		}else{
			$this->error('无效提交！');
		}
	}
	
	public function Authorityadds()
	{
		$data['code'] = $this->_post('code');//标示
		$data['module'] = $this->_post('module');//控制器
		$data['action'] = $this->_post('action');//方法
		$data['name'] = $this->_post('name');//权限名称
		$data['parent'] = $this->_post('parent');//上级权限
		$data['url'] = $this->_post('url');//地址
		$data['memo'] = $this->_post('memo');//描述
		$data['is_show'] = $this->_post('is_show');//显示
		$data['is_menu'] = $this->_post('is_menu');//菜单
		$data['sort'] = $this->_post('sort');//顺序
		$data['input_time'] = time();
		$data['input_ip'] = $this->fj_getIP();
		$power = $this->power->add($data);
		
		$role = $this->role->field('role_id')->select();
		$ip = $this->fj_getIP();
		$time = time();
		$date['status'] = '0';
		$powers = array();
		$keys = 0;
		foreach ($role as $key=>$v)
		{
			$powers[$keys]['power_id'] = $power;
			$powers[$keys]['role_id'] = $v['role_id'];
			$powers[$keys]['input_time'] = $time;
			$powers[$keys]['input_ip'] = $ip;
			$powers[$keys]['status'] = 0;
			$keys++;
		}
		$role_power = $this->role_power->addAll($powers);
		if(false !== $power AND false !==$role_power)
		{
			$this->admin_log('add','power','无','1');//日志文件
			$this->success('数据写入成功！');
		}
		else
		{
			$this->admin_log('add','power','无','0');//日志文件
			$this->error('数据写入错误！');
		}
	}
	
	public function Authorityadd() //添加权限
	{
		if($parent=$this->_get('parent'))
		{
			$this->assign('parent',$parent);
		}
		$powera = array();
		$powers = $this->power->where('is_show=1 AND parent=0')->field('id,name')->select();
		foreach($powers as $key=>$v)
		{
			$powerss = $this->power->where('parent='.$v['id'])->field('id,name')->select();
			$powera[$key]['id'] = $v['id'];
			$powera[$key]['name'] = $v['name'];
			foreach ($powerss as $keys=>$vs)
			{
				$powera[$key]['parent'][$vs['id']]['id'] = $vs['id'];
				$powera[$key]['parent'][$vs['id']]['name'] = $vs['name'];
			}
		}
		$this->assign('powera',$powera);
		$this->display();
	}
	
	public function Authorityeds()
	{
		if($id=$this->_get('id'))
		{
			$power = $this->power->where('id='.$id)->field('id')->find();
			if($power['id'])
			{
				$data['code'] = $this->_post('code');//标示
				$data['module'] = $this->_post('module');//控制器
				$data['action'] = $this->_post('action');//方法
				$data['name'] = $this->_post('name');//权限名称
				$data['parent'] = $this->_post('parent');//上级权限
				$data['url'] = $this->_post('url');//地址
				$data['memo'] = $this->_post('memo');//描述
				$data['is_show'] = $this->_post('is_show');//显示
				$data['is_menu'] = $this->_post('is_menu');//菜单
				$data['sort'] = $this->_post('sort');//顺序
				$power = $this->power->where('id='.$id)->data($data)->save();
				if(false !== $power)
					{
						$this->admin_log('ed','power','无','1');//日志文件
						//$this->assign ( "jumpUrl", $this->getReturnUrl () );
						$this->assign ( 'jumpUrl', Cookie( '_currentUrl_' ) );
						$this->success ( '数据写入成功！' );
					}
					else
					{
						$this->admin_log('ed','power','无','0');//日志文件
						$this->error ( '数据写入错误！' );
					}
			}else{
				$this->error ( '无效提交！' );
			}
		}else{
			$this->error ( '无效提交！' );
		}
	}
	
	public function Authorityed()
	{
		if($id=$this->_get('id'))
		{
			$power = $this->power->where('id='.$id)->find();
			if($power['id']){
				$this->assign($power);
				$powera = array();
				$powers = $this->power->where('is_show=1 AND parent=0')->field('id,name')->select();
				foreach($powers as $key=>$v)
				{
					$powerss = $this->power->where('parent='.$v['id'])->field('id,name')->select();
					$powera[$key]['id'] = $v['id'];
					$powera[$key]['name'] = $v['name'];
					foreach ($powerss as $keys=>$vs)
					{
						$powera[$key]['parent'][$vs['id']]['id'] = $vs['id'];
						$powera[$key]['parent'][$vs['id']]['name'] = $vs['name'];
					}
				}
				//print_r($powera);
				$this->assign('powera',$powera);
				$this->display();
			}else{
				$this->error('无效提交' );
			}
		}else{
			$this->error('无效提交');
		}
	}
	
	public function Authorityck()
	{
		$id = $this->_post('id');
		if($id){
			$power = $this->power->where('id='.$id)->find();
			$parent = '';
			if($power['id']){
				if($power['parent']>0)
				{
					$powers = $this->power->where('id='.$power['parent'])->field('name,parent')->find();
					if($powers['parent']>0)
					{
						$parent = $powers['name'].'-'.$power['name'];
						$powerss = $this->power->where('id='.$powers['parent'])->field('name,parent')->find();
						if($powerss['id']>0)
						{
							$parent = $powerss['name'].'-'.$powers['name'].'-'.$power['name'];
						}
					}
				}else{
					$parent = $parent.$power['name'];
				}
				if($power['is_show']>0)
				{
					$is_show = '开启'; 
				}else{
					$is_show = '关闭'; 
				}
				echo "{'qxcw':'1','qxcws':'提交成功','name':'".$power['name']."','code':'".$power['code']."','parent':'".$parent."','input_time':'".date('Y-m-d',$power['input_time'])."','memo':'".$power['memo']."','input_ip':'".$power['input_ip']."','is_show':'".$is_show."','sort':'".$power['sort']."'}";
			}else{
				echo "{'qxcw':'0','qxcws':'无效提交'}";
			}
		}else{
			echo "{'qxcw':'0','qxcws':'无效提交'}";
		}
	}
	
	public function Authority() //权限列表页面
	{
		/*import('ORG.Util.Page');// 导入分页类
		$count      = $this->role_power->where('status=1')->count();// 查询满足要求的总记录数
		$Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性*/
		/*$power = $this->power->select();
		$powers = array();
		foreach ($power as $key=>$v)
		{
			if($v['parent']==0)
			{
				$powers[$v['id']]['code'] = $v['code'];
				$powers[$v['id']]['name'] = $v['name'];
				$powers[$v['id']]['id'] = $v['id'];
				$powers[$v['id']]['input_time'] = $v['input_time'];
			}else{
				$powers[$v['parent']]['parent'][$v['id']]['code'] = $v['code'];
				$powers[$v['parent']]['parent'][$v['id']]['name'] = $v['name'];
				$powers[$v['parent']]['parent'][$v['id']]['id'] = $v['id'];
				$powers[$v['parent']]['parent'][$v['id']]['input_time'] = $v['input_time'];
			}
		}*/
		$powers = array();
		$power = $this->power->where('parent=0')->order('sort')->select();
		foreach ($power as $key=>$v)
		{
			$powers[$key]['code'] = $v['code'];
			$powers[$key]['name'] = $v['name'];
			$powers[$key]['input_ip'] = $v['input_ip'];
			$powers[$key]['url'] = $v['url'];
			$powers[$key]['sort'] = $v['sort'];
			$powers[$key]['is_show'] = $v['is_show'];
			$powers[$key]['id'] = $v['id'];
			$powers[$key]['input_time'] = $v['input_time'];
			$powera = $this->power->where('parent='.$v['id'])->order('sort')->select();
			if($powera[0]['id']>0){
				foreach ($powera as $keys=>$vs){
					if($v['id']!=$powera[$keys]['id']){
						$powers[$key]['parent'][$vs['id']]['code'] = $vs['code'];
						$powers[$key]['parent'][$vs['id']]['name'] = $vs['name'];
						$powers[$key]['parent'][$vs['id']]['input_ip'] = $vs['input_ip'];
						$powers[$key]['parent'][$vs['id']]['url'] = $vs['url'];
						$powers[$key]['parent'][$vs['id']]['sort'] = $vs['sort'];
						$powers[$key]['parent'][$vs['id']]['is_show'] = $vs['is_show'];
						$powers[$key]['parent'][$vs['id']]['id'] = $vs['id'];
						$powers[$key]['parent'][$vs['id']]['names'] = $v['name'];
						$powers[$key]['parent'][$vs['id']]['input_time'] = $vs['input_time'];
						$powerb = $this->power->where('parent='.$vs['id'])->order('sort')->select();
						if($powerb[0]['id']>0){
							foreach ($powerb as $keyss=>$vss){
								if($vs['id']!=$powerb[$keys]['id']){
									$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['code'] = $vss['code'];
									$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['name'] = $vss['name'];
									$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['input_ip'] = $vss['input_ip'];
									$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['url'] = $vss['url'];
									$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['sort'] = $vss['sort'];
									$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['is_show'] = $vss['is_show'];
									$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['id'] = $vss['id'];
									$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['names'] = $v['name'];
									$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['namess'] = $vs['name'];
									$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['input_time'] = $vss['input_time'];
								}
							}
						}
					}
				}
			}
		}
		$this->assign('power',$powers);
		$this->display();
	}
	public function Authoritys()
	{
		$role = $this->role->order('sort')->select();
		$this->assign(role,$role);
		$this->display();
	}
	public function Authoritysadd()
	{
		$this->display();
	}
	public function Authoritysadds()
	{
		$data['name'] = $this->_post('name');
		$data['content'] = $this->_post('content');
		$data['sort'] = $this->_post('sort');
		$data['status'] = $this->_post('status');
		$data['input_time'] = time();
		$data['input_times'] = time();
		$data['input_ip'] = $this->fj_getIP();		
		$role = $this->role->add($data);
		if($role>0){
			$power = $this->power->field('id,is_show')->select();
			$powers = array();
			foreach ($power as $key=>$v)
			{
				//$role_power = $this->role_power->data($data)>add();
				$powers[$key]['role_id'] = $role;
				$powers[$key]['power_id'] = $v['id'];
				$powers[$key]['status'] = '0';
				$powers[$key]['input_time'] = time();
				$powers[$key]['input_ip'] = $data['input_ip'];
			}
			$role_power = $this->role_power->addAll($powers);
		}
	   	if(false !== $role)
		{
			$this->admin_log('add','role','无','1');//日志文件
			$this->success('数据写入成功！','/Admin/index.php/Expand/Authoritys',1);
		}else{
			$this->admin_log('add','role','无','0');//日志文件
			$this->error('数据写入错误','',0);
		}
	}
	public function Authoritysed()
	{
		if($role_id = $this->_get('id')){
			$role = $this->role->where('role_id='.$role_id)->find();
			$this->assign($role);
			$this->display();
		}else{
			$this->error('非法请求','',0);	
		}
	}
	public function Authorityseds()
	{
		if($role_id = $this->_get('id')){
			$role = $this->role->where('role_id='.$role_id)->find();
			if($role['role_id']){
				$data['name'] = $this->_post('name');
				$data['content'] = $this->_post('content');
				$data['sort'] = $this->_post('sort');
				$data['status'] = $this->_post('status');
				$data['input_times'] = time();
				$roles = $this->role->where('role_id='.$role_id)->data($data)->save();
			   	if(false !== $roles)
				{
					$this->admin_log('ed','role','无','1');//日志文件
					$this->success('数据写入成功！');
				}else{
					$this->admin_log('ed','role','无','0');//日志文件
					$this->error('数据写入错误','',0);
				}
			}else{
				$this->error('非法请求','',0);
			}
		}else{
			$this->error('非法请求','',0);	
		}
	}
	public function Authoritysdel()
	{
		if($role_id = $this->_get('id')){
			$role = $this->role->where('role_id='.$role_id)->find();
			if($role['role_id']){
				$roles = $this->role->where('role_id='.$role_id)->delete();
				$roless = $this->role_power->where('role_id='.$role_id)->delete(); 
				if(false !== $roles AND false !==$roless)
				{
					$this->admin_log('del','role','无','1');//日志文件
					$this->success('数据写入成功！');
				}else{
					$this->admin_log('del','role','无','0');//日志文件
					$this->error('数据写入错误');
				}
			}else{
				$this->error('非法请求');	
			}
		}else{
			$this->error('非法请求');	
		}
	}
	public function Authoritysset()
	{
		if($role_id = $this->_get('id')){
			$role = $this->role->where('role_id='.$role_id)->find();
			if($role['role_id']){
				$this->assign($role);
				$powers = array();
				$power = $this->power->where('parent=0 AND is_show=1')->order('sort')->select();
				foreach ($power as $key=>$v)
				{
					$powers[$key]['code'] = $v['code'];
					$powers[$key]['name'] = $v['name'];
					$powers[$key]['input_ip'] = $v['input_ip'];
					$powers[$key]['url'] = $v['url'];
					$powers[$key]['sort'] = $v['sort'];
					$powers[$key]['is_show'] = $v['is_show'];
					$powers[$key]['id'] = $v['id'];
					$powers[$key]['input_time'] = $v['input_time'];
					$powera = $this->power->where('parent='.$v['id'].' AND is_show=1')->order('sort')->select();
					if($powera[0]['id']>0){
						foreach ($powera as $keys=>$vs){
							if($v['id']!=$powera[$keys]['id']){
								$powers[$key]['parent'][$vs['id']]['code'] = $vs['code'];
								$powers[$key]['parent'][$vs['id']]['name'] = $vs['name'];
								$powers[$key]['parent'][$vs['id']]['input_ip'] = $vs['input_ip'];
								$powers[$key]['parent'][$vs['id']]['url'] = $vs['url'];
								$powers[$key]['parent'][$vs['id']]['sort'] = $vs['sort'];
								$powers[$key]['parent'][$vs['id']]['is_show'] = $vs['is_show'];
								$powers[$key]['parent'][$vs['id']]['id'] = $vs['id'];
								$powers[$key]['parent'][$vs['id']]['names'] = $v['name'];
								$powers[$key]['parent'][$vs['id']]['input_time'] = $vs['input_time'];
								$powerb = $this->power->where('parent='.$vs['id'].'  AND is_show=1')->order('sort')->select();
								if($powerb[0]['id']>0){
									foreach ($powerb as $keyss=>$vss){
										if($vs['id']!=$powerb[$keys]['id']){
											$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['code'] = $vss['code'];
											$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['name'] = $vss['name'];
											$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['input_ip'] = $vss['input_ip'];
											$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['url'] = $vss['url'];
											$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['sort'] = $vss['sort'];
											$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['is_show'] = $vss['is_show'];
											$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['id'] = $vss['id'];
											$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['names'] = $v['name'];
											$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['namess'] = $vs['name'];
											$powers[$key]['parent'][$vs['id']]['parent'][$vss['id']]['input_time'] = $vss['input_time'];
										}
									}
								}
							}
						}
					}
				}
				$this->assign('power',$powers);
				$role_power = $this->role_power->where('role_id='.$role_id)->field('power_id,status')->select();
				$role_powers = array();
				foreach ($role_power as $key=>$v)
				{
						$role_powers[$v['power_id']] = $v['status'];
				}
				$this->assign('role_powers',$role_powers);
				$this->display();
			}
		}else{
			$this->error('非法请求','',0);	
		}		
	}
	public function Authorityssets()
	{
		$role_id = $this->_get('role_id');
		/*$role_power = $this->role_power->where('role_id='.$role_id)->field('id')->select();
		$power = $this->power->where('is_show=1')->field('id')->select();
		$powers = array();
		foreach ($power as $key=>$v)
		{
			$powers[]=$v['id'];
		}
		$powerss = array_diff($power,$role_power);*/
		$role_powers = $this->role_power->where('role_id='.$role_id)->data('status=0')->save();
		foreach ($_POST['checkbox'] as $key=>$v)
		{
			$this->role_power->where('power_id='.$v)->data('status=1')->save();
		}
		if(false !== $role_powers)
		{
			$this->admin_log('del','role','无','1');//日志文件
			$this->success('数据写入成功！');
		}else{
			$this->admin_log('del','role','无','0');//日志文件
			$this->error('数据写入错误');
		}
	}
	
	public function Authorityscy()
	{
		if(!$id = $this->_get('id')){
			$this->error("非法访问!");	
		}
		$user = $this->user->where('role='.$id)->select();
		$this->assign('user',$user);
		$this->assign('roleid',$id);
		$this->display();
	}
	
	public function Authorityscys()
	{
		$user = $this->user->where('`name` LIKE \'%'.$this->_post('name').'%\'')->field('name,u_id')->select();
		
		$users = '';
		foreach ($user as $key => $v){
			$users .= '<option value="'.$v['u_id'].'">'.$v['name'].'</option>';
		}
		echo '<select id="u_id" name="u_id">'.$users.'</select>';
		
	}
	
	public function Authorityscyadd()
	{
		if(!$id = $this->_get('id') OR !$uid = $this->_post('u_id')){
			echo 1;
			exit();
		}
		$users = $this->user->where('u_id='.$uid)->field('name,u_id,role')->find();
		if(!$users['u_id']){
			echo 1;
			exit();
		}
		if($users['role'] == $id){
			echo 2;
			exit();
		}
		$user = $this->user->where('u_id='.$uid)->data('role='.$id)->save();
		if(false !== $user){
			echo '<tr><td>'.$users['u_id'].'</td><td>'.$users['name'].'</td></tr>';
		}else{
			echo 1;
			exit();
		}	
	}
	//安全问题
	public function Safequestion(){
		$this->authorityss('74');
		$question = $this->safe_question->order('id desc')->select();
		$this->assign('question',$question);
		$this->display();
	}
	//添加问题
	public function Questionadd(){
		$this->authorityss('75');
		$this->authorityss('76');
		if($wid = $this->_get('wid')){
			$questiona = $this->safe_question->where('id = '.$wid)->find();	
			$this->assign($questiona);
		}
		if($this->_post('dosubmit')){				
			$data['question'] = $this->_post('question');
			$data['type'] = $this->_post('type')=='1' ? '1' : '2';
			if(!empty($data['question'])){
				echo $this->_post('questionid');
				if($questionid = $this->_post('questionid')){
					//更新
					$adds= $this->safe_question->where('id='.$questionid)->save($data);
					if($adds){
						$this->admin_log('save','web_safe_question','更新安全问题成功','1');//日志文件
						$this->success('更新成功');
					}else{
						$this->admin_log('save','web_safe_question','更新安全问题失败','2');//日志文件
						$this->error('更新失败');
					}
				}else{
					//添加
					$add = $this->safe_question->data($data)->add();
					if($add){
						$this->admin_log('add','web_safe_question','添加安全问题成功','1');//日志文件
						$this->success('添加成功');
					}else{
						$this->admin_log('add','web_safe_question','添加安全问题失败','2');//日志文件
						$this->error('添加失败');
					}
				}
			}else{
				$this->error('问题不能为空');
			}
		}
		$this->display();
	}
	//底部栏目列表
	public function Topiclist(){
		$this->authorityss('78');
		$topiclist = $this->about_us->select();
		$this->assign('topiclist',$topiclist);
		$this->display();
	}
	//栏目添加
	public function Topicadd(){
		$this->authorityss('79');
		$this->authorityss('80');
		if($e_id = $this->_get('e_id')){
			$con = $this->about_us->where('id='.$e_id)->find();
			$this->assign($con);
		}
		if(!empty($_POST['dosubmit'])){
			if(($name = $this->_post('name')) && ($con = $_POST['content'])){
				$datas['name'] = trim($name,' ');
				$datas['content'] = $con;
				//$a_id 不为空，执行更新
				if($a_id = $this->_post('a_id')){			
					$datas['edit_time'] = time();
					$adds = $this->about_us->where('id='.$a_id)->save($datas);
					if($adds){
						$this->admin_log('save','web_about_us','更新栏目成功','1');//日志文件
						$this->success('更新成功');
					}else{
						$this->admin_log('save','web_about_us','更新栏目失败','1');//日志文件
						$this->error('更新失败');
					}
				}else{
					$datas['input_time'] = time();
					$add = $this->about_us->data($datas)->add();
					if($add){
						$this->admin_log('add','web_about_us','添加栏目成功','1');//日志文件
						$this->success('添加成功');
					}else{
						$this->admin_log('add','web_about_us','添加栏目失败','2');//日志文件
						$this->error('添加失败');
					}
				}
			}else{
				$this->error('栏目名或栏目内容不能为空！');
			}	
		}
		$this->display();
	}
	//删除栏目
	public function Topicdel(){
		$this->authorityss('81');
		if($d_id = $this->_get('d_id')){
			$del = $this->about_us->where('id='.$d_id)->delete();
			if($del){
				$this->admin_log('del','web_about_us','删除栏目成功','1');//日志文件
				$this->success('删除成功');
			}else{
				$this->admin_log('del','web_about_us','删除栏目失败','2');//日志文件
				$this->error('删除失败');
			}
		}else{
			$this->error('非法操作');
		}
	}
	
}
?>